package com.hyopt.modules.app.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyopt.modules.app.dao.SysCustomerInfoDao;
import com.hyopt.modules.app.entity.SysCustomerInfo;
import com.hyopt.modules.app.service.SysCustomerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @PROJECT hyopt-new
 * @ClassName SysCompanyServiceImpl
 * @Description IntelliJ IDEA
 * @Author Li9527
 * @Date 2022/9/8
 * @Version 1.0
 */
@Service
@Slf4j
public class SysCustomerInfoServiceImpl extends ServiceImpl<SysCustomerInfoDao, SysCustomerInfo> implements SysCustomerInfoService {

    @Autowired
    SysCustomerInfoDao sysCustomerInfoDao;

    /**
     * @Author Li9527
     * @Description 同步智慧城环客户数据
     * @Date 2022/9/8 15:17
     * @Param [token]
     * @return void
     **/
    @Override
    public void getZhchInfo(String token) {
        String url = "http://wxxinsoft.gnway.org:11807/api/system/sysCustomerInfo/findListByParam";
        HttpRequest httpRequest = HttpUtil.createPost(url);
        httpRequest.header("token",token);
        httpRequest.body("{\"customerName\":\"\",\"customerCom\":\"\",\"currentPage\":1,\"pageSize\":100000000}");
        JSONObject responseJson = null;
        HttpResponse response = httpRequest.execute();
        if (response.getStatus() == 200) {
            String body = response.body();
            responseJson = JSONObject.parseObject(body);
            log.info("获取客户信息成功{}" + responseJson);
            String value = JSONObject.parseObject(responseJson.get("data").toString()).get("records").toString();
            JSONArray finalJson = JSONArray.parseArray(value);
            if(finalJson.size() >0){
                // 清除原来表里的数据
                sysCustomerInfoDao.truncateTable();
                for (int i = 0; i < finalJson.size(); i++) {
                    JSONObject jsonObject = finalJson.getJSONObject(i);
                    SysCustomerInfo sysCustomerInfo = jsonObject.toJavaObject(SysCustomerInfo.class);
                    sysCustomerInfoDao.insert(sysCustomerInfo);
                }
            }
        }else{
            log.info("获取客户信息失败");
        }
        log.info("定时任务正在执行，执行时间：{}", DateUtil.now().toString());
    }
}
